<template>
  <el-card shadow="hover" style="width: 350px">
    <template #header>
      <div class="option-header">
        <div class="option-header-content"><span>双障碍期权</span></div>
        <div v-if="props.calcSuccess=='true'" class="option-header-content"><span style="color: #22bb98 ">计算成功</span></div>
        <!--        <div v-if="props.calcSuccess=='false'" class="option-header-content"><span style="color: #e53c3c">计算失败</span></div>-->
        <div v-if="props.calcSuccess=='false'" class="option-header-content">
          <el-tooltip  effect="light"  >
            <template #content> <div style="width: 280px;">
              <span style="color: #e53c3c;">{{props.errorMsg}}</span>
            </div> </template>
            <span style="color: #e53c3c">计算失败</span>
          </el-tooltip>
        </div>
        <div>
          <el-button style="background: transparent;" :disabled="cpoyWait" v-if="props.useType=='calc'" class="delete" @click="copy" title="复制">
            <el-icon>
              <copy-document/>
            </el-icon>
          </el-button>
          <el-button style="background: transparent;" class="delete" @click="cancel" title="关闭">
            <el-icon>
              <circle-close/>
            </el-icon>
          </el-button>
        </div>
      </div>
    </template>

    <el-form ref="formRef" :disabled="form.data.readOnly" :model="form.data" label-width="auto" size="small"
             :rules="rules">
      <div class="optionst01">
        <el-form-item label="交易方向" prop="payDirection">
          <el-radio-group v-model="form.data.payDirection" size="small" :fill="getColor()">
            <el-radio-button label="1">买入</el-radio-button>
            <el-radio-button label="-1">卖出</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="标的" label-width="auto" prop="instrumentId">
          <Underlyer ref="underlyerIdRef" :filterDate="true" v-model="form.data.instrumentId" @change="onBlurInstrumentId" :underlyerId="form.data.instrumentId"/>
        </el-form-item>
        <!--合约乘数-->
        <!--      todo-->
        <!--涨跌类型-->
        <el-form-item label="涨跌类型" label-width="auto" prop="optionType">
          <el-radio-group v-model="form.data.optionType" size="small">
            <el-radio label="Call">看涨</el-radio>
            <el-radio label="Put">看跌</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="起始日" label-width="auto" prop="effectiveDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.effectiveDate" @blur="onBlurEffectiveDate" type="date" placeholder="">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="期限" label-width="auto" prop="term">
          <Numberinput v-model="form.data.term" append="D"/>
        </el-form-item>

        <el-form-item label-width="auto" label="到期日" prop="terminationDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.terminationDate" @blur="onBlurTerminationDate" type="date" placeholder=""/>
        </el-form-item>

        <el-form-item label="结算偏移" label-width="auto" prop="paymentDaysOffset">
          <Numberinput v-model="form.data.paymentDaysOffset" append="D"/>

        </el-form-item>

        <el-form-item label-width="auto" label="到期支付日" prop="terminationDate">
          <el-date-picker value-format="YYYY-MM-DD" v-model="form.data.terminationPaymentDate"
                          type="date"
                          placeholder=""/>
        </el-form-item>

        <el-form-item label="名义本金" prop="notional">
          <InputAmount v-model:value="form.data.notional" append="元" precision="2"></InputAmount>
        </el-form-item>


        <el-form-item label="期初价格" label-width="auto">
          <el-col :span="8">
            <el-checkbox name="status" false-label="0" true-label="1" v-model="form.data.priceOnWait">待定</el-checkbox>
          </el-col>
          <el-col :span="16">
            <InputAmount v-model:value="form.data.spotPrice" :disabled="form.data.priceOnWait=='1'"
                         append="元" precision="2"/>
          </el-col>
        </el-form-item>

        <el-form-item label="交易数量" prop="optionEntitlement">
          <Numberinput v-model="form.data.optionEntitlement" precision="2"  :disabled="form.data.priceOnWait=='1'" @change="optionEntitlementChange"/>
        </el-form-item>
        <el-form-item  label="补偿参与率" prop="rebateParticipationRate">
          <Numberinput v-model="form.data.rebateParticipationRate" precision="2" append="%"/>
        </el-form-item>
        <el-form-item  label="参与率" prop="participationRate">
          <Numberinput v-model="form.data.participationRate" precision="2" append="%"/>
        </el-form-item>
      </div>
      <!---------------------------------------------- 分割线---------------------------------------------------------->
      <div class="optionst02">
        <el-form-item label="行权价比例" prop='strikePercentage'>
          <Numberinput v-model="form.data.strikePercentage" @blur="linkPrice(form,form.data.strike,form.data.spotPrice,'data.strikePercentage')" precision="2" append="%"/>
        </el-form-item>
        <el-form-item label="行权价" prop='strike'>
          <InputAmount v-model:value="form.data.strike" @blur="linkPercentage(form,form.data.strikePercentage,form.data.spotPrice,'data.strike')"
                       append="元" precision="2"/>
        </el-form-item>
        <el-form-item label="敲出方向" prop='knockType'>
          <dictCombox v-model="form.data.knockType"
                      dictType="optionTypeTb" filterKey="DoubleKnockOut,DoubleKnockIn" ></dictCombox>
        </el-form-item>
        <el-form-item label="支付时机" prop='payStyle'>
          <dictCombox v-model="form.data.payStyle"
                      dictType="compensateDay"  ></dictCombox>
        </el-form-item>

        <el-form-item label="观察方式" prop='scheduleType'>
          <dictCombox v-model="form.data.scheduleType"
                      dictType="scheduleType"  filterKey="0,1"></dictCombox>
        </el-form-item>
        <el-form-item label="低障碍" prop='lowTriggerRate'>
          <el-col :span="12"><InputAmount v-model:value="form.data.lowTriggerPrice" @blur="onLowTriggerPriceChange" precision="2" append="元"/></el-col>
          <el-col :span="12"><Numberinput v-model="form.data.lowTriggerRate" @blur="onLowTriggerRateChange" precision="2" append="%"/></el-col>
        </el-form-item>
<!--        <el-form-item label="低障碍价"  prop='lowTriggerPrice'>
        </el-form-item>-->
        <el-form-item label="高障碍" prop='triggerRate'>
          <el-col :span="12"><InputAmount v-model:value="form.data.triggerPrice" @blur="onTriggerPriceChange" precision="2" append="元"/></el-col>
          <el-col :span="12"><Numberinput v-model="form.data.triggerRate" @blur="onTriggerRateChange" precision="2" append="%"/></el-col>
        </el-form-item>
<!--        <el-form-item label="高障碍价"  prop='triggerPrice'>
        </el-form-item>-->
        <el-form-item label="是否年化" >
          <dictCombox v-model="form.data.moneynessOption"
                      dictType="boolType"
                      :style="{width: '100%'}"></dictCombox>
        </el-form-item>
        <el-form-item label="低补偿" prop='lowRebateRate'>
          <el-col :span="12"><InputAmount v-model:value="form.data.lowRebatePrice"  @blur="onLowRebatePriceChange" precision="2" append="元"/></el-col>
          <el-col :span="12"><Numberinput v-model="form.data.lowRebateRate" @blur="onLowRebateRateChange" precision="2" append="%"/></el-col>
        </el-form-item>
<!--        <el-form-item label="低补偿收益"  prop='lowRebatePrice' v-show="true">
        </el-form-item>-->
        <el-form-item label="高补偿" prop='rebateRate'>
          <el-col :span="12"><InputAmount v-model:value="form.data.rebatePrice"  @blur="onRebatePriceChange" precision="2" append="元"/></el-col>
          <el-col :span="12"><Numberinput v-model="form.data.rebateRate" @blur="onRebateRateChange" precision="2" append="%" /></el-col>
        </el-form-item>
<!--        <el-form-item label="高补偿收益"   prop='rebatePrice' v-show="true">
        </el-form-item>-->
        <el-form-item v-show="false" label="计息天数">
          <Numberinput v-model="form.data.term" append="D"/>
        </el-form-item>

        <el-form-item v-show="form.data.moneynessOption =='1'" label="计息基准">
          <dictCombox v-model="form.data.daycounter" prop='disDayCount'
                      dictType="dayCounter"
                      :style="{width: '100%'}"></dictCombox>
        </el-form-item>

        <el-form-item class="operation">
          <el-button type="primary" @click="calc" v-show="props.useType=='calc'||props.useType=='scene'"
                     :disabled="form.data.priceOnWait=='1'">计算
          </el-button>
          <el-button @click="save2CalcLog" v-show="props.useType=='calc'" :disabled="form.data.priceOnWait=='1'">存为草稿</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="optionst03">
      <PricingParam ref="PricingParamRef" :formData="form.data"  :valueDate="props.valueDate"  :exerciseType="form.data.exerciseType" :index="props.index"
                    label-width="auto" :filterList="form.data.filterList" :disabled="form.data.readOnly"
                    :productType="productType" size="small"
                    :calcPricingParam="props.calcPricingParam"></PricingParam>
    </div>
    <div v-show="props.useType!='scene'">
      <singleResult :result="form.result" :index="props.index"></singleResult>
    </div>
  </el-card>
</template>

<script setup>
import {linkPercentage,linkPrice}  from '@/views/calc/otcOtion/instrument/common/formula'
import {getCurrentInstance, onMounted, onUpdated, provide, reactive, ref, toRaw, toRefs, watch, nextTick} from 'vue'
import Underlyer from '@/xquant/base/underlyer'
import Numberinput from '@/xquant/base/number'
import InputAmount from '@/xquant/base/InputAmount'
import dictCombox from '@/xquant/base/dictCombox'
import PricingParam from '@/views/calc/otcOtion/pricingparam/index'
import singleResult from '@/views/calc/otcOtion/result/single'
import {getTradeDay} from '@/utils/calc'
import {formartFormP4, formartTableP2, unFormartNumber, formartFormP2, numberPrecision} from '@/utils/numberUtil'
import {
  isVoid,
  isNotVoid,
  isVoidArg,
  isNotVoidArg,
  calcAnnualizedByTerminationPaymentDate,
  isEquativesABC,
  isEquativesAnnualizedABC,
  mul,
  add,
  sub,
  div,
  isEqualNum,
  mulArg,
  round,
} from '@/utils/util'
import {getTerminationPaymentDate, getOffset} from '@/api/calc/otcOption/calc'
import {debounce} from 'lodash'
import {setCommonSpotPrice} from '@/views/calc/otcOtion/instrument/common/common'
import * as Util from "../../../../utils/util";
import {formartNumber} from "../../../../utils/numberUtil";
import {validateDates} from "./common/validate";

const cpoyWait = ref(false)
const getColor = () => {
  return form.data.payDirection == '1' ? '#E53C3C' : '#22BB98'
}
 const {proxy} = getCurrentInstance()
const productType = ref('DoubleBarrier')

const labelWidth = ref('111px')
const form = reactive({
  data: {
    productType: productType.value,
    payDirection: '1',
    instrumentId: null,
    optionType: 'Call',
    effectiveDate: proxy.$moment(new Date()).format('YYYY-MM-DD'),
    terminationDate: '',
    terminationPaymentDate: '',
    term: '30',
    scheduleType:'1',
    paymentDaysOffset: '0',
    priceOnWait: '0',
    spotPrice: '',
    strikePercentage: '100',
    strike: 0,
    notional: '',
    optionEntitlement: '',
    participationRate: 100,
    calendar: 'CHINA_EX',
    daycounterTerm: '365',
    exerciseType: '',
    isIncludeEndDate: '',
    endDate: '',
    isIncludeStartDate: '',
    startDate: '',
    moneynessOption: '1',
    daycounter: 'Actual/365',
    knockType:'DoubleKnockOut',
    lowTriggerRate:'100',
    lowTriggerPrice:'',
    triggerRate:'110',
    triggerPrice:'',
    lowRebateRate:'100',
    lowRebatePrice:'',
    rebateRate:'100',
    rebatePrice:'',
    fixedRate: '0',
    payStyle:'Deferred',//支付时机  Deferred 到期支付
    rebateParticipationRate:'100' //补偿参与率
  },
  result: {},
  initFlag: ''
})
const rules = {
  payDirection: [{required: true, message: '请选择交易方向', trigger: 'change'}],
  instrumentId: [{required: true, message: ' ', trigger: 'change'}],
  optionType: [{required: true, message: '请选择涨跌类型', trigger: 'change'}],
  effectiveDate: [{required: true, message: ' ', trigger: 'change'}],
  terminationDate: [{required: true, message: ' ', trigger: 'change'}],
  term: [{required: true, message: ' ', trigger: 'change'}],
  paymentDaysOffset: [{required: true, message: ' ', trigger: 'change'}],
  scheduleType:[{required: true, message: ' ', trigger: 'change'}],
  notional: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  daycounter: [{required: true, message: ' ', trigger: 'change'}],
  daycounterTerm: [{required: true, message: ' ', trigger: 'change'}],
  spotPrice: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  optionEntitlement: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  strike: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  strikePercentage: [{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      if (form.data.priceOnWait == '1') {
        return true
      }
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  participationRate: [{required: true, message: ' ', trigger: 'change'}],
  knockType: [{required: true, message: ' ', trigger: 'change'}],
  lowTriggerRate:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  lowTriggerPrice:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  triggerRate:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  triggerPrice:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  lowRebateRate:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  lowRebatePrice:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  rebateRate:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
  rebatePrice:[{required: true, message: ' ', trigger: 'change'}, {
    validator: (rule, value, callback) => {
      return Number(value) > 0
    }, message: ' ', trigger: 'change'
  }],
}

watch(() => props.result, (nv, ov) => {
  let other = {
    'type': '双障碍期权',
    '标的': computed(() => {
      if (proxy.$refs.underlyerIdRef&&proxy.$refs.underlyerIdRef.underlyerIdsList[0]) {
        return (proxy.$refs.underlyerIdRef.underlyerIdsList[0].productName+'').trim().split(' ').join(',')
      }
    }),
    '交易日期': props.valueDate,
    '合约类型': form.data.optionType == 'Call' ? 'C' : 'P',
    '合约交易方向': form.data.payDirection == '1' ? '客户卖价' :  '客户买价',
    '到期支付日': form.data.terminationDate,
    '敲出方向':form.data.knockType=='DoubleKnockOut'?'双敲出期权':'双敲入期权',
    '执行价': formartFormP2(form.data.strike),
    '高障碍价格': formartFormP2(form.data.triggerPrice),
    '低障碍价格': formartFormP2(form.data.lowTriggerPrice),
    '高补偿比例': formartFormP2(form.data.rebateRate),
    '低补偿比例': formartFormP2(form.data.lowRebateRate),
    '数量': formartFormP2(form.data.optionEntitlement),
    '名义金额': formartFormP2(form.data.notional),
    '开仓标的价': formartFormP2(form.data.spotPrice),
    '期权单价': computed(() => {
      if (nv) {
        return formartFormP2(nv.npv / form.data.optionEntitlement)
      }
    }),
    '百分比估值': computed(() => {
      if (nv) {
        return formartFormP2(100 * nv.npv / form.data.notional)
      }
    }),
  }
  form.result = Object.assign(other, nv)
}, {immediate: true})


watch(() => form.data.priceOnWait, (nv, ov) => {
  if (nv == '1') {
    form.data.spotPrice = 0
  } else {
    setSpotPrice()
  }
})
/*
* 修改状态  false:当前没有操作  true:当前在被其他方法操作
* */
const lock = reactive({
  paymentDaysOffset: false,
  terminationPaymentDate: false,
})
watch(() => form.data.paymentDaysOffset, (nv, ov) => {
  if (nv === '') return
  if (lock.terminationPaymentDate) return
  lock.terminationPaymentDate = true
  onpaymentDaysOffsetChange()
})
const onpaymentDaysOffsetChange = debounce(function () {
  getTerminationPaymentDate({
    'calDay': form.data.terminationDate,
    'offset': form.data.paymentDaysOffset
  }).then(response => {
    lock.terminationPaymentDate = false
    if (response.errorCode != 0) {

    } else {
      if (response.result) {
        form.data.terminationPaymentDate = response.result.CAL_DAY
      }
    }
  })
}, 1000)
watch(() => form.data.terminationPaymentDate, (nv, ov) => {
  if (nv == ov || nv === '') return
  if (lock.paymentDaysOffset) return
  lock.paymentDaysOffset = true
  onterminationPaymentDate()
})
const onterminationPaymentDate = debounce(function (nv, ov) {
  getOffset({
    'day': form.data.terminationPaymentDate,
    'calDay': form.data.terminationDate
  }).then(response => {
    if (response.errorCode != 0) {

    } else {
      if (response.result == null) {
        onpaymentDaysOffsetChange()
        return
      }
      form.data.paymentDaysOffset = response.result.i
      lock.paymentDaysOffset = false
    }
  })
}, 1000)
onUnmounted(() => {
  // 移除组件时，取消定时器
  onpaymentDaysOffsetChange.cancel()
  onterminationPaymentDate.cancel()
})
const onBlurTerminationDate= (nv, ov) => {
  let a = proxy.$moment(form.data.terminationDate)
  let b = proxy.$moment(form.data.terminationPaymentDate)
  form.data.paymentDaysOffset = b.diff(a, 'days')
}
watch(() => [form.data.effectiveDate, form.data.terminationDate], ([nv, n2v], [ov, o2v]) => {
  if (!proxy.lsh.isEmpty(nv) && !proxy.lsh.isEmpty(n2v)) {
    let a = proxy.$moment(n2v)
    let b = proxy.$moment(nv)
    form.data.term = Number(a.diff(b, 'days'))
   proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }
})
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isNotVoid(String(form.data.effectiveDate)) && isNotVoid(String(newValue))) {
    let a = proxy.$moment(form.data.effectiveDate)
    form.data.terminationDate = a.add(newValue, 'days').format('YYYY-MM-DD')
    onpaymentDaysOffsetChange()
  }
})
/**
 * 1.1
 * 名义本金  联动 行权价 交易数量
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(newValue, form.data.spotPrice, form.data.optionEntitlement)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.spotPrice))) {
      if (isEqualNum(form.data.spotPrice, 0)) {
        return
      }
      form.data.optionEntitlement = newValue / form.data.spotPrice
      return
    }
    if (isNotVoid(String(form.data.optionEntitlement))) {
      if (isEqualNum(form.data.optionEntitlement, 0)) {
        return
      }
      form.data.spotPrice = newValue / form.data.optionEntitlement
      return
    }
  }
})
/**
 * 1.2
 * 期初价格  联动 名义本金 交易数量
 * */
watch(() => [form.data.spotPrice], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(form.data.notional, newValue, form.data.optionEntitlement)) {
    return
  }

  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.notional))) {
      if (isEqualNum(form.data.spotPrice, 0)) {
        form.data.optionEntitlement = 0
        return
      }
      form.data.optionEntitlement = form.data.notional / newValue
      return
    }
    if (isNotVoid(String(form.data.optionEntitlement))) {
      form.data.notional = form.data.optionEntitlement * newValue
      return
    }
  }
})
/**
 * 1.3
 * 交易数量 联动 期初价格 名义本金
 * */
const optionEntitlementChange = (newValue) => {
  if (form.data.priceOnWait == '1') return
  if (isVoid(newValue)) {
    return
  }
  if (optionEntitlementLinkValid(form.data.notional, form.data.spotPrice, newValue)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (isNotVoid(String(form.data.spotPrice))) {
      form.data.notional = newValue * form.data.spotPrice
      return
    }
    if (isNotVoid(String(form.data.notional))) {
      form.data.spotPrice = form.data.notional / newValue
      return
    }
  }
}

/**
 *
 * 交易数量  期初价格 名义本金 联动 等式
 * */
function optionEntitlementLinkValid(notional, spotPrice, optionEntitlement) {
  if (isNotVoid(notional) && isNotVoid((spotPrice)) && isNotVoid(optionEntitlement)) {
    return isEquativesABC(optionEntitlement, spotPrice, notional)
  }
  return false
}

/**
 * 2.1
 * 期初价格  联动 行权比例  行权价
 * */
watch(() => [form.data.spotPrice], ([newValue], [oldValue]) => {
  linkPercentage(form,form.data.strikePercentage,form.data.spotPrice,'data.strike')
})


/**
 * 2.3
 * 高障碍价  联动 行权比例  行权价
 * */
watch(() => [form.data.spotPrice], ([newValue], [oldValue]) => {
  linkPercentage(form,form.data.strikePercentage,form.data.spotPrice,'data.strike')
})





/**
 * 3.1  高
 *  期限 监听 高行权收益率 收益金额  是否年化  名义本金  计息基准
 * */
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.rebateRate, form.data.rebatePrice, form.data.daycounter, newValue)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.notional))) {

        if (isNotVoid(String(form.data.rebateRate))) {
          if (isEqualNum(0, form.data.rebatePrice)) {
            form.data.rebatePrice = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.rebatePrice = (form.data.rebateRate / 100) * annualized * form.data.notional
            return
          }
        }
        if (isNotVoid(String(form.data.rebatePrice))) {
          if (isEqualNum(0, form.data.rebatePrice)) {
            form.data.rebateRate = 0
            return
          } else {
            form.data.rebateRate = form.data.rebatePrice * 100 / annualized / form.data.notional
            return
          }
        }
      }
    }
  }
})
/**
 * 3.2  高
 *  计息基准 监听 高行权收益率 收益金额  是否年化  名义本金  期限
 * */
watch(() => [form.data.daycounter], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.rebateRate, form.data.rebatePrice, newValue, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(String(form.data.rebateRate))) {
          if (isEqualNum(form.data.rebateRate, 0)) {
            form.data.rebatePrice = 0
            return
          } else {
            form.data.rebatePrice = form.data.rebateRate * (annualized * form.data.notional) / 100
            return
          }
        }
        if (isVoid(String(form.data.rebateRate)) && isNotVoid(String(form.data.rebatePrice))) {
          if (isEqualNum(form.data.rebatePrice, 0)) {
            form.data.rebateRate = 0
            return
          } else {
            form.data.rebateRate = form.data.rebatePrice * 100 / (annualized * form.data.notional)
            return
          }
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.rebateRate))) {

        if (isEqualNum(form.data.rebateRate, 0)) {
          form.data.rebatePrice = 0
          return
        } else {
          form.data.rebatePrice = form.data.rebateRate * form.data.notional / 100
          return
        }
      }
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.rebatePrice))) {
        if (isEqualNum(form.data.rebatePrice, 0)) {
          form.data.rebateRate = 0
          return
        } else {
          form.data.rebateRate = form.data.rebatePrice * 100 / form.data.notional
          return
        }
      }
    }
  }

})
/**
 * 3.3  高
 *  收益金额 监听 高行权收益率 计息基准  是否年化  名义本金  期限
 * */
watch(() => [form.data.rebatePrice], ([newValue], [oldValue]) => {
  return
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.rebateRate, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.rebateRate = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.notional)) {
            form.data.rebateRate = 0
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            let percentages = newValue * 100 / (annualized * form.data.notional)
            form.data.rebateRate = percentages * 100
            return
          }
        }
      }
      if (isNotVoid(String(form.data.rebateRate)) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.rebateRate)) {
            form.data.notional = 0
          } else {
            let percentages = form.data.rebateRate / 100
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.notional = (newValue / percentages) / annualized
            return
          }
        }

      }

    } else {
      if (isNotVoid(String(form.data.notional))) {
        form.data.rebateRate = newValue * 100 / form.data.notional
        return
      }
      if (isNotVoid(String(form.data.rebateRate))) {
        if (isEqualNum(0, form.data.rebateRate)) {
          form.data.notional = 0
        } else {
          let percentages = form.data.rebateRate / 100
          form.data.notional = newValue / percentages
          return
        }
      }

    }
  }
})

/**
 * 3.4 高
 * 名义本金 监听 高行权收益率 收益金额  是否年化 计息基准  期限
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, newValue, form.data.rebateRate, form.data.rebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(form.data.rebateRate) && isVoid(String(form.data.rebatePrice))) {
          if (isEqualNum(0, form.data.rebateRate)) {
            form.data.rebatePrice = 0
            return
          } else {
            let percentages = form.data.rebateRate / 100
            form.data.rebatePrice = percentages * annualized * newValue
            return
          }
        }
        if (isNotVoid(String(form.data.rebateRate)) && isNotVoid(String(form.data.rebatePrice))) {
          if (isEqualNum(0, form.data.rebateRate)) {
            form.data.rebatePrice = 0
            return
          } else {
            let percentages = form.data.rebateRate / 100
            form.data.rebatePrice =percentages * annualized * newValue
            return
          }
        }
        if (isVoid(String(form.data.rebateRate)) && isNotVoid(String(form.data.rebatePrice))) {
          if (isEqualNum(0, form.data.rebatePrice)) {
            form.data.rebateRate = 0
            return
          } else {
            form.data.rebateRate = form.data.rebatePrice * 100 / (annualized * newValue)
            return
          }
        }
      }
    }
  } else {
    if (isNotVoid(String(form.data.rebateRate))) {
      if (isEqualNum(0, form.data.rebateRate)) {
        form.data.rebatePrice = 0
        return
      } else {
        form.data.rebatePrice = form.data.rebateRate / 100 * newValue
        return
      }
    }
  }
})

/**
 * 3.5 高
 *   是否年化 监听  行权收益率 收益金额   名义本金  计息基准
 * */
watch(() => [form.data.moneynessOption], ([newValue], [oldValue]) => {
  if (paymentAmountLinkValid(newValue, form.data.notional, form.data.rebateRate, form.data.rebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (newValue == '1') {
    if (isNotVoidArg(String(form.data.notional), String(form.data.daycounter), String(form.data.term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      if (isNotVoid(String(form.data.rebateRate))) {
        if (!isEqualNum(0, form.data.rebateRate) && !isEqualNum(0, form.data.rebatePrice)) {
          form.data.rebatePrice = form.data.rebateRate / 100 * annualized * form.data.notional
          return
        } else {
          form.data.rebatePrice = 0
          return
        }
      }
      if (isNotVoid(String(form.data.rebatePrice))) {
        if (!isEqualNum(0, form.data.rebatePrice) && !isEqualNum(0, form.data.rebatePrice)) {
          form.data.rebateRate = form.data.rebatePrice * 100 / (annualized * form.data.notional)
          return
        } else {
          form.data.rebateRate = 0
          return
        }
      }

    }
  } else {
    if (isNotVoidArg(String(form.data.notional), String(form.data.rebateRate))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.rebatePrice = form.data.rebateRate / 100 * form.data.notional
        return
      } else {
        form.data.rebatePrice = 0
        return
      }
    }
    if (isNotVoid(String(form.data.notional), String(form.data.rebatePrice))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.rebatePrice)) {
        form.data.rebateRate = form.data.rebatePrice * 100 / form.data.notional
        return
      } else {
        form.data.rebateRate = 0
        return
      }
    }
  }

})

/**
 * 3
 *  高行权收益率 收益金额 计息基准  是否年化  名义本金  期限  等式
 * */
function paymentAmountLinkValid(moneynessOption, notional, paymentAmountPecentage, paymentAmount, daycounter, term) {
  if (moneynessOption == '1') {
    if (isNotVoid(String(notional)) && isNotVoid(String(paymentAmountPecentage)) && isNotVoid(String(paymentAmount)) && isNotVoid(String(term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      let actualPercentages = proxy.lsh.divide(paymentAmountPecentage , 100)
      var isTrue = isEquativesAnnualizedABC(notional, actualPercentages, annualized, paymentAmount)
      return isTrue
    }
  } else {
    if (isNotVoid(String(notional)) && isNotVoid(String(paymentAmountPecentage)) && isNotVoid(String(paymentAmount))) {
      let actualPercentages = proxy.lsh.divide(paymentAmountPecentage , 100)
      var isTrue = isEquativesABC(actualPercentages, notional, paymentAmount)
      return isTrue
    }
  }
}

/**
 * 4.1  低
 *  期限 监听 高行权收益率 收益金额  是否年化  名义本金  计息基准
 * */
watch(() => [form.data.term], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.lowRebateRate, form.data.lowRebatePrice, form.data.daycounter, newValue)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.notional))) {

        if (isNotVoid(String(form.data.lowRebateRate))) {
          if (isEqualNum(0, form.data.lowRebatePrice)) {
            form.data.lowRebatePrice = 0
            return
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.lowRebatePrice = form.data.lowRebateRate * annualized * form.data.notional / 100
            return
          }
        }
        if (isNotVoid(String(form.data.lowRebatePrice))) {
          if (isEqualNum(0, form.data.lowRebatePrice)) {
            form.data.lowRebateRate = 0
            return
          } else {
            form.data.lowRebateRate = 100 * form.data.lowRebatePrice / (annualized * form.data.notional)
            return
          }
        }
      }
    }
  }

})
/**
 * 4.2  低
 *  计息基准 监听 高行权收益率 收益金额  是否年化  名义本金  期限
 * */
watch(() => [form.data.daycounter], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.lowRebateRate, form.data.lowRebatePrice, newValue, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(String(form.data.lowRebateRate))) {
          if (isEqualNum(form.data.lowRebateRate, 0)) {
            form.data.lowRebatePrice = 0
            return
          } else {
            form.data.lowRebatePrice = form.data.lowRebateRate / 100 * annualized * form.data.notional
            return
          }
        }
        if (isNotVoid(String(form.data.lowRebatePrice))) {
          if (isEqualNum(form.data.lowRebatePrice, 0)) {
            form.data.lowRebateRate = 0
            return
          } else {
            form.data.lowRebateRate = form.data.lowRebatePrice * 100 / (annualized * form.data.notional)
            return
          }
        }
      }
    } else {
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.rebateRate))) {
        if (isEqualNum(form.data.lowRebateRate, 0)) {
          form.data.lowRebatePrice = 0
          return
        } else {
          form.data.lowRebatePrice = form.data.lowRebateRate * form.data.notional / 100
          return
        }
      }
      if (isNotVoid(String(form.data.notional)) && isNotVoid(String(form.data.lowRebatePrice))) {
        if (isEqualNum(form.data.lowRebatePrice, 0)) {
          form.data.lowRebateRate = 0
          return
        } else {
          form.data.lowRebateRate = form.data.lowRebatePrice * 100 / form.data.notional
          return
        }
      }
    }
  }

})


/**
 * 4.4  低
 * 名义本金 监听 高行权收益率 收益金额  是否年化 计息基准  期限
 * */
watch(() => [form.data.notional], ([newValue], [oldValue]) => {
  if (isVoid(newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, newValue, form.data.lowRebateRate, form.data.lowRebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (form.data.moneynessOption == '1') {
    if (isNotVoid(String(newValue))) {
      if (isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        if (isNotVoid(form.data.lowRebateRate)) {
          if (isEqualNum(0, form.data.lowRebateRate)) {
            form.data.lowRebatePrice = 0
            return
          } else {
            let percentages = form.data.lowRebateRate / 100
            form.data.lowRebatePrice = percentages * annualized * newValue
            return
          }
        }
        if (isNotVoid(String(form.data.lowRebatePrice))) {
          if (isEqualNum(0, form.data.lowRebatePrice)) {
            form.data.lowRebateRate = 0
            return
          } else {
            form.data.lowRebateRate = form.data.lowRebatePrice * 100 / (annualized * newValue)
            return
          }
        }
      }
    }
  } else {
    if (isNotVoid(String(form.data.lowRebateRate))) {
      if (isEqualNum(0, form.data.lowRebateRate)) {
        form.data.lowRebatePrice = 0
        return
      } else {
        form.data.lowRebatePrice = form.data.lowRebateRate * newValue / 100
        return
      }
    }
  }
})

/**
 * 4.5  低
 *   是否年化 监听  行权收益率 收益金额   名义本金  计息基准
 * */
watch(() => [form.data.moneynessOption], ([newValue], [oldValue]) => {
  if (paymentAmountLinkValid(newValue, form.data.notional, form.data.lowRebateRate, form.data.lowRebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (newValue == '1') {
    if (isNotVoidArg(String(form.data.notional), String(form.data.daycounter), String(form.data.term))) {
      let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
      if (isNotVoid(String(form.data.lowRebateRate))) {
        if (isEqualNum(0, form.data.lowRebateRate)) {
          form.data.lowRebatePrice = 0
          return
        } else {
          form.data.lowRebatePrice = form.data.lowRebateRate / 100 * (annualized * form.data.notional)
          return
        }
      }
      if (isNotVoid(String(form.data.lowRebatePrice))) {
        if (isEqualNum(0, form.data.lowRebatePrice)) {
          form.data.lowRebateRate = 0
          return
        } else {
          form.data.lowRebateRate = form.data.lowRebatePrice * 100 / (annualized * form.data.notional)
          return
        }
      }

    }
  } else {
    if (isNotVoidArg(String(form.data.notional), String(form.data.lowRebateRate))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.lowRebatePrice = form.data.lowRebateRate / 100 * form.data.notional
        return
      } else {
        form.data.lowRebatePrice = 0
        return
      }
    }
    if (isNotVoidArg(String(form.data.lowRebatePrice), String(form.data.lowRebateRate))) {
      if (!isEqualNum(0, form.data.notional) && !isEqualNum(0, form.data.notional)) {
        form.data.lowRebateRate = form.data.lowRebatePrice * 100 / form.data.notional
        return
      } else {
        form.data.lowRebateRate = 0
        return
      }
    }
  }

})

/** 高障碍价*/
const onTriggerPriceChange = () => {
  let a =  form.data.triggerPrice;
  if (Util.isNotVoid(String(form.data.spotPrice))) {
    form.data.triggerRate = proxy.lsh.multiply(proxy.lsh.divide(a, form.data.spotPrice), 100)
    return
  }
}

const onTriggerRateChange = () => {
  let a = form.data.triggerRate;
  if (Util.isNotVoid(String(form.data.spotPrice))) {
    form.data.triggerPrice = proxy.lsh.multiply(proxy.lsh.divide(a, 100), form.data.spotPrice)
    return
  }
}

/** 低障碍价*/
const onLowTriggerPriceChange = () => {
  let a = form.data.lowTriggerPrice;
  if (Util.isNotVoid(String(form.data.spotPrice))) {
    form.data.lowTriggerRate =proxy.lsh.multiply(proxy.lsh.divide(a, form.data.spotPrice), 100)
    return
  }
}

const onLowTriggerRateChange = () => {
  let a = form.data.lowTriggerRate;
  if (Util.isNotVoid(String(form.data.spotPrice))) {
    form.data.lowTriggerPrice = proxy.lsh.multiply(proxy.lsh.divide(a, 100), form.data.spotPrice)
    return
  }
}
/**高补偿收益*/
/**
 * 3.3  高
 *  收益金额 监听 高行权收益率 计息基准  是否年化  名义本金  期限
 * */
const onRebatePriceChange = () => {
  let newValue = form.data.rebatePrice
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.rebateRate, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.rebateRate = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.notional)) {
            form.data.rebateRate = 0
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            let percentages = newValue * 100 / (annualized * form.data.notional)
            form.data.rebateRate = percentages
            return
          }
        }
      }
      if (isNotVoid(String(form.data.rebateRate)) && !isEqualNum(form.data.rebateRate, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.rebateRate)) {
            form.data.notional = 0
          } else {
            let percentages = form.data.rebateRate / 100
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.notional = newValue / percentages / annualized
            return
          }
        }

      }
    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        form.data.rebateRate = newValue * 100 / form.data.notional
        return
      }
      if (isNotVoid(String(form.data.rebateRate)) && !isEqualNum(form.data.rebateRate, 0)) {
        if (isEqualNum(0, form.data.rebateRate)) {
          form.data.notional = 0
        } else {
          let percentages = form.data.rebateRate / 100
          form.data.notional = newValue / percentages
          return
        }
      }

    }
  }
}
/**
 * 3.3  高
 *  高行权收益率  监听 收益金额 计息基准  是否年化  名义本金  期限
 * */
const onRebateRateChange = () => {
  let newValue = form.data.rebateRate
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, newValue, form.data.rebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term)) && isNotVoid(String(newValue))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        form.data.rebatePrice = proxy.lsh.round(newValue * annualized * form.data.notional / 100,2)
        return
      }
      if (isNotVoid(String(form.data.rebatePrice)) && !isEqualNum(form.data.rebatePrice, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
          form.data.notional = form.data.rebatePrice / newValue * 100 / annualized
          return
        }
      }

    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.rebatePrice = 0.00
          return
        } else {
          form.data.rebatePrice = proxy.lsh.round(form.data.notional * newValue / 100,2)
          return
        }
      }
      if (isNotVoid(String(form.data.rebatePrice)) && !isEqualNum(form.data.rebatePrice, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          form.data.notional = form.data.rebatePrice * 100 / newValue
          return
        }
      }

    }
  }
}

/**低补偿收益*/
const onLowRebatePriceChange = () => {
  let newValue = form.data.lowRebatePrice
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, form.data.lowRebateRate, newValue, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.lowRebateRate = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.notional)) {
            form.data.lowRebateRate = 0
          } else {
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            let percentages = newValue * 100 / (annualized * form.data.notional)
            form.data.lowRebateRate = percentages
            return
          }
        }
      }
      if (isNotVoid(String(form.data.lowRebateRate)) && !isEqualNum(form.data.lowRebateRate, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          if (isEqualNum(0, form.data.lowRebateRate)) {
            form.data.notional = 0
          } else {
            let percentages = form.data.lowRebateRate / 100
            let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
            form.data.notional = newValue / percentages / annualized
            return
          }
        }

      }
    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        form.data.lowRebateRate = newValue * 100 / form.data.notional
        return
      }
      if (isNotVoid(String(form.data.lowRebateRate)) && !isEqualNum(form.data.lowRebateRate, 0)) {
        if (isEqualNum(0, form.data.lowRebateRate)) {
          form.data.notional = 0
        } else {
          let percentages = form.data.lowRebateRate / 100
          form.data.notional = newValue / percentages
          return
        }
      }

    }
  }
}

const onLowRebateRateChange = () => {
  let newValue = form.data.lowRebateRate
  if (isVoid(newValue)) {
    return
  }
  if (isEqualNum(0, newValue)) {
    return
  }
  if (paymentAmountLinkValid(form.data.moneynessOption, form.data.notional, newValue, form.data.lowRebatePrice, form.data.daycounter, form.data.term)) {
    return
  }
  if (isNotVoid(String(newValue))) {
    if (form.data.moneynessOption == '1') {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term)) && isNotVoid(String(newValue))) {
        let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
        form.data.lowRebatePrice = proxy.lsh.round(newValue * annualized * form.data.notional / 100,2)
        return
      }
      if (isNotVoid(String(form.data.lowRebatePrice)) && !isEqualNum(form.data.lowRebatePrice, 0) && isNotVoid(String(form.data.daycounter)) && isNotVoid(String(form.data.term))) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          let annualized = calcAnnualizedByTerminationPaymentDate(form.data)
          form.data.notional = form.data.lowRebatePrice / newValue * 100 / annualized
          return
        }
      }

    } else {
      if (isNotVoid(String(form.data.notional)) && !isEqualNum(form.data.notional, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.lowRebatePrice = 0.00
          return
        } else {
          form.data.lowRebatePrice = proxy.lsh.round(form.data.notional * newValue / 100,2)
          return
        }
      }
      if (isNotVoid(String(form.data.lowRebatePrice)) && !isEqualNum(form.data.lowRebatePrice, 0)) {
        if (isEqualNum(0, newValue)) {
          form.data.notional = 0.00
          return
        } else {
          form.data.notional = form.data.lowRebatePrice * 100 / newValue
          return
        }
      }

    }
  }
}


/**
 * 获取 定价配置 和  标的价格
 * */
watch(() => [props.cfgId,form.data.instrumentId], ([newId, oldId]) => {
  getPricingCfg()
  setTimeout(() => {
    proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }, 1000);
})

const onBlurInstrumentId = () => {
  setSpotPrice()
  getPricingCfg()
  setTimeout(() => {
    proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }, 1000);
}
watch(() => props.valueDate, (newValue, oldValue) => {
  if (newValue != oldValue) {
    setSpotPrice()
    //触发修改定价参数的价格
    proxy.$refs.PricingParamRef.setSpotPrice({
      valueDate:props.valueDate,
      instrumentId:form.data.instrumentId
    })
   proxy.$refs.PricingParamRef?proxy.$refs.PricingParamRef.calcRateByCode():''
  }
})


watch(() => form.data.exerciseType, (nv, ov) => {
  if (form.data.exerciseType == 'American') {
    form.data.startDate = form.data.effectiveDate
    form.data.endDate = form.data.terminationDate
  }
}, {
  immediate: true
})
const props = defineProps({
  /*是否为重放*/
  isReCalc: {
    type: Boolean,
    default: false
  },
  calcSuccess:{
    type: String,
    default: ''
  },
  errorMsg:{
    type: String,
    default: ''
  },
  useType: {
    type: String,
    default: 'calc'
  },
  index: {
    type: String,
    default: ''
  },
  del: {
    type: Function,
    default: null
  },
  copy: {
    type: Function,
    default: null
  },
  calc: {
    type: Function,
    default: null
  },
  save2Trade: {type: Function, default: null},
  save2CalcLog: {type: Function, default: null},
  getDefaultCfg: {
    type: Function,
    default: null
  },
  calcPricingParam: {
    type: Object,
    default: {}
  },
  result: {
    type: Object,
    default: {}
  },
  setformData: {
    type: Function,
    default: null
  },
  formData: {
    type: Object,
    default: null
  },
  cfgId: {type: String, default: ''},
  valueDate: {type: String, default: ''}
})
const getResult = () => {
  var data = []
  for (var item in props.result) {
    if (['npv', 'delta', 'gamma', 'vega', 'theta', 'rho', 'spotPrice', 'dscRate'].includes(item)) {
      data.push({
        key: item,
        val: proxy.lsh.ceil(props.result[item], 4)
      })
    }
  }
  return data
}
//定价参数
const getPricingCfg = async () => {
  let query = {
    'calcPricingParam': {
      'cfgId': props.cfgId
    },
    'instrument': {
      'productType': productType.value,
      'termsheet': {
        'underlyerIds': [form.data.instrumentId],
        'customLegs': [{
          'legId': 'leg01',
          'productType': productType.value
        }
        ],
        'legs': [{
          'legId': 'leg01',
          'payDirection': '1'
        }
        ]
      }
    },
  }
  //设置定价参数中的定盘价
  proxy.$refs.PricingParamRef.setSpotPrice({
    valueDate:props.valueDate,
    instrumentId:form.data.instrumentId
  })
  if (props.getDefaultCfg != null) {
    props.getDefaultCfg(query, props.index)
  }
}
const cancel = () => {
  props.del(props.index)
}
const copy = () => {
  props.copy(props.index)
  cpoyWait.value = true;
  setTimeout(() => {
    cpoyWait.value = false;
  }, 1000);
}
const calc = (flag) => {
  /*flag 标识 为All时单次计算不会提示成功或失败*/
  getInstrumentJson().then((res) => {
    if (Boolean(res)) {
      let otcOption = res.otcOption
      otcOption.calcInstrumentForm = res.calcInstrumentForm
      otcOption.calcPricingForm = res.calcPricingForm
      if (otcOption != '') {
        props.calc(otcOption, props.index, flag)
      }
    }
  })
}
const getInstrumentJson = async () => {
  return new Promise((resolve) => {
    proxy.$refs['formRef'].validate(valid => {
      if (valid) {
        validateDates(form)
        if (Number(form.data.triggerRate) <= Number(form.data.lowTriggerRate)) {
          proxy.$modal.alertWarning('高障碍价必须大于低障碍价,请核对!')
          return
        }
        let calcPriceParam = getCalcPriceParam()
        let otcOption = {}
          // form.data.productType = 'Digital'
          //普通二元
          otcOption = {
            instrument: {
              'termsheet': {
                'notional': form.data.notional,
                'notionalCurrency': 'CNY',
                'settlementType': 'Cash',
                'settlementCurrency': 'CNY',
                'effectiveDate': form.data.effectiveDate,
                'effectiveDateConv': 'NONE',
                'terminationDate': form.data.terminationDate,
                'terminationDateConv': 'NONE',
                'terminationDateCalendar': form.data.calendar,
                'underlyerIds': [
                  form.data.instrumentId
                ],
                'calendars': [
                  form.data.calendar
                ],
                'legs': [
                  {
                    'legId': 'leg01',
                    'payDirection': form.data.payDirection
                  }
                  // ,
                  // {
                  //   'legId': 'leg02',
                  //   'payDirection': Number(form.data.payDirection)*-1
                  // }
                ],
                'customLegs':[
                  {
                    "legId": "leg01",
                    "productType": form.data.knockType,
                    "optionType":  form.data.optionType ,
                    "exercise": {
                      "type": "European",
                      "dates": [
                        form.data.terminationDate
                      ],
                      'conv': 'NONE',
                      'calendar': form.data.calendar
                    },
                    "underlyers": [
                      {
                        "instrumentId":form.data.instrumentId,
                        "spotPrice": form.data.spotPrice,
                        "quoteType": "Close",
                        "fixingDate": form.data.terminationDate,
                        "fixingDateConv": "Preceding",
                        "fixingDateCalendar": form.data.calendar
                      }
                    ],
                    "knocks": [
                      {
                        "trigger": {
                          "type":  'EqualOrLess' ,
                          "level": "",
                          "levelPercentage": proxy.lsh.divide(form.data.lowTriggerRate,100)
                        },
                        "observeDates": {
                          "startDate": form.data.effectiveDate ,
                          "startDateConv": "NONE",
                          "endDate": form.data.terminationDate,
                          "endDateConv": "NONE",
                          "calendar":  form.data.calendar,
                          "frequency": form.data.scheduleType=='1'?'1D':'0D',
                          "discreteDates": []
                        },
                        "observeType": "Close",
                        "participationRate": proxy.lsh.divide(form.data.rebateParticipationRate,100),
                        "paymentAmount": "",
                        "paymentAmountPecentage": proxy.lsh.divide(form.data.lowRebateRate,100),
                        "paymentDate": form.data.terminationPaymentDate,
                        "paymentDateOffset": "0D",
                        "paymentDateConv": "NONE",
                        "paymentDateCalendar": form.data.calendar,
                        "payStyle": form.data.payStyle
                      },
                      {
                        "trigger": {
                          "type":  'EqualOrGreater',
                          "level": "",
                          "levelPercentage": proxy.lsh.divide(form.data.triggerRate,100)
                        },
                        "observeDates": {
                          "startDate": form.data.effectiveDate ,
                          "startDateConv": "NONE",
                          "endDate": form.data.terminationDate,
                          "endDateConv": "NONE",
                          "calendar":  form.data.calendar,
                          "frequency": form.data.scheduleType=='1'?'1D':'',
                          "discreteDates": []
                        },
                        "observeType": "Close",
                        "participationRate":  proxy.lsh.divide(form.data.rebateParticipationRate,100),
                        "paymentAmount": "",
                        "paymentAmountPecentage": proxy.lsh.divide(form.data.rebateRate,100),
                        "paymentDate": form.data.terminationPaymentDate,
                        "paymentDateOffset": "0D",
                        "paymentDateConv": "NONE",
                        "paymentDateCalendar":  form.data.calendar,
                        "payStyle": form.data.payStyle
                      }
                    ],
                    "strike": "",
                    "strikePercentage": formartNumber(form.data.strikePercentage/100,2),
                    "numberOfOptions": form.data.moneynessOption == '1'?calcAnnualizedByTerminationPaymentDate(form.data):"1",
                    "optionEntitlement":  form.data.spotPrice == '0' ? form.data.optionEntitlement : form.data.notional / form.data.spotPrice,
                    "moneynessOption": 0,
                    // "moneynessOption": form.data.moneynessOption,
                    // "daycounter": form.data.moneynessOption == '1' ? form.data.daycounter : '',
                    "periodOfAccrualInterestAdjust": '0',
                    "participationRate": proxy.lsh.divide(form.data.participationRate,100),
                    "fixingRounding": "-1",
                    "settlementDate": form.data.terminationPaymentDate,
                    "settlementDateOffset": form.data.paymentDaysOffset+"D",
                    "settlementDateConv": "NONE",
                    "settlementDateCalendar":  form.data.calendar,
                  }
                  // ,
                  // {
                  //   "legId": "leg02",
                  //   "productType": "Premium",
                  //   "paymentDate": "2019-03-13",
                  //   "paymentConv": "NONE",
                  //   "paymentAmount": "6.39",
                  //   "paymentCurrency": "CNY"
                  // }
                ]
              },
              'productType':form.data.knockType,
              'initProductType': productType.value
            },
            calcPricingParam: {
              valueDate: props.valueDate,
              cfgId: props.cfgId,
              calcPPCode: calcPriceParam.calcPPCode,
              underlyingPricingParams: [
                {
                  underlyingID: form.data.instrumentId,
                  dividend: calcPriceParam.dividend,
                  volSurface: calcPriceParam.volSurface,
                  funding: calcPriceParam.discount,
                  spotPrice: calcPriceParam.spotPrice == '0' ? '' : calcPriceParam.spotPrice,
                  spotQuoteType: calcPriceParam.spotQuoteType
                }
              ],
              discount: calcPriceParam.discount,
              'customPricingParam': {}
            },
            underlyingMarkets: [
              {
                'underlyingID': form.data.instrumentId,
                'fixingPrices': [],
                'observerPrices': []
              }
            ]
          }

        let calcInstrumentForm = toRaw(form.data)
        let result = props.result
        let json = {
          otcOption: otcOption,
          calcInstrumentForm: calcInstrumentForm,
          calcPricingForm: calcPriceParam,
          calcResultForm: result
        }
        resolve(json)
      } else {
        resolve('')
      }

    })
  })
}

/**
 *保存 交易
 * */
const save2Trade = () => {
  getInstrumentJson().then((res) => {
    if (Boolean(res)) {
      props.save2Trade(res)
    }
  })

}
/**
 *保存 草稿
 * */
const save2CalcLog = () => {
  getInstrumentJson().then((res) => {
    if (Boolean(res)) props.save2CalcLog(res)
  })
}
/**
 * 获取定价参数
 * */
const getCalcPriceParam = () => {
  let calcPriceParam = proxy.$refs.PricingParamRef.getCalcPricingParam()
  return calcPriceParam
}

const getFormData = () => {
  return form
}
/**
 *获取价格 价格类型
 * */
const setSpotPrice = () => {
  setCommonSpotPrice(proxy,form,props)
}

const onBlurEffectiveDate = () => {
  form.initFlag = false
  setSpotPrice()
}

defineExpose({
  calc, getPricingCfg, getCalcPriceParam, getFormData, getInstrumentJson,proxy
})
onMounted(() => {
  form.initFlag = props.isReCalc
  if (props.formData != null) {
    form.initFlag = isNotVoid(props.calcPricingParam)
    let data = props.formData.data || props.formData
    form.data = Object.assign(form.data, data)
  }
  // 这里获取DOM的value是旧值
  nextTick(()=>{
    form.initFlag = false
  })
})
watch(() => proxy.$attrs.disabled, (nv) => {
  form.data.readOnly = nv
}, {
  immediate: true
})
watch(()=>form,()=>{

  proxy.$attrs.cacheFormData()
},{deep:true})
</script>
<style>

</style>

